package xdu.lz.stage6_threadpool.chapter3_threadPoolExecutorAPI;

import java.util.concurrent.*;

/**
 * Created at 2019/10/24 0024 上午 11:31
 *
 * 提交无返回值的任务
 * void execute(Runnable)
 * Future<?> submit(Runnable task);
 * <T> Future<T> submit(Runnable task, T result);
 * <p>
 * 提交有返回值的任务
 * <T> Future<T> submit(Callable<T> task);
 * 看源码，以上提交方式，最后都是给execute执行的
 */
public class RunnableTask {

    public static void main (String[] args) throws ExecutionException, InterruptedException {

        runnableTest();
    }


    /**
     *
     * {@link Executor#execute(Runnable)}
     * {@link ExecutorService#submit(Runnable)}
     * {@link ExecutorService#submit(Runnable, Object)} )}
     * {@link ExecutorService#submit(Callable)}
     * @return
     */
    private static ExecutorService runnableTest() throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(5);
        //1 void execute(Runnable)
        executor.execute(()->{
            System.out.println("task 1");
        });
        //2 Future<?> submit(Runnable)
        Future<?> future = executor.submit(() -> {
            System.out.println("task2");
        });
        try {
            //调用get会阻塞住，直到结果完成，返回的也是null
            Object NULL = future.get();
            System.out.println("NULL = " + NULL);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        //3 <T> Future<T> submit(Runnable,T result)
        String notify = "DONE";
        Future<String> future1 = executor.submit(() -> {
            System.out.println("task3");
        }, notify);
        try {
            //get会阻塞住，结果完成会输出notify
            System.out.println("future1 = " + future1.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        //4 提交有返回值的

        Future<String> submit = executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(1000);
                return "done";
            }
        });
        System.out.println("submit = " + submit.get());
        return executor;
    }

    //当任务执行结束后，设定回收coreSize，自动关闭线程池
    private static ExecutorService newFixedThreadPool4() {
        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
        //这个参数必须设定大于0
        executor.setKeepAliveTime(1, TimeUnit.SECONDS);
        executor.allowCoreThreadTimeOut(true);
        executor.submit(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        executor.submit(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        return executor;
    }


}
